package com.swap.business.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    public static final String SWAP_EXCHANGE_NAME = "swap_exchange";
    public static final String SWAP_QUEUE_NAME = "swap_queue";
    public static final String SWAP_QUEUE_REMOVE_NAME = "swap_remove_queue";
    private static final String routing_key01 = "swap.queue";
    private static final String routing_key02 = "swap.remove";

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public Exchange swapExchange() {
        return ExchangeBuilder.topicExchange(SWAP_EXCHANGE_NAME).build();
    }

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue simpleQueue(){
        return QueueBuilder.durable(SWAP_QUEUE_NAME).build();
    }


    @Bean
    public Queue simpleRemoveQueue(){
        return QueueBuilder.durable(SWAP_QUEUE_REMOVE_NAME).build();
    }

    /**
     * 绑定交换机与队列
     * @param exchange
     * @param queue
     * @return
     */
    @Bean
    public Binding itemQueueExchange(@Qualifier("swapExchange") Exchange exchange, @Qualifier("simpleQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(routing_key01).noargs();
    }

    @Bean
    public Binding itemQueueExchange1(@Qualifier("swapExchange") Exchange exchange, @Qualifier("simpleRemoveQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(routing_key02).noargs();
    }
}
